package com.zzyl.nursing.service.impl;

import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.mapper.UserMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 报警数据Service业务层处理
 * 
 * @author alexis
 * @date 2025-01-15
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return alertDataMapper.selectById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return alertDataMapper.insert(alertData);
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return alertDataMapper.updateById(alertData);
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return alertDataMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return alertDataMapper.deleteById(id);
    }

    @Override
    public TableDataInfo<AlertData> selectAllAlertData(AlertDataDto alertDataDto, Long userId) {
        Date startTime = null;
        Date endTime = null;
        if(ObjectUtil.isNotEmpty(alertDataDto.getStartTime()) || ObjectUtil.isNotEmpty(alertDataDto.getEndTime())){
            startTime = Date.from(alertDataDto.getStartTime().atZone(ZoneId.systemDefault()).toInstant());
            endTime = Date.from(alertDataDto.getEndTime().atZone(ZoneId.systemDefault()).toInstant());
        }
        Page<AlertData> page = new Page<>(alertDataDto.getPageNum(), alertDataDto.getPageSize());
        LambdaQueryWrapper<AlertData> queryWrapper = Wrappers.<AlertData>lambdaQuery().like(alertDataDto.getDeviceName() != null, AlertData::getDeviceName, alertDataDto.getDeviceName())
                .between(alertDataDto.getStartTime() != null && alertDataDto.getEndTime() != null, AlertData::getCreateTime, startTime, endTime)
                .eq(alertDataDto.getStatus() != null, AlertData::getStatus, alertDataDto.getStatus())
                .eq(AlertData::getUserId, userId);

        page = page(page, queryWrapper);
        TableDataInfo<AlertData> tableDataInfo = creatTableData(page);
        return tableDataInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int handleAlertData(AlertData alertData,Long userId) {
        String name = userMapper.selectNameById(userId);
        alertData.setProcessorId(userId);
        alertData.setProcessorName(name);
        alertData.setStatus(1);
        int result = alertDataMapper.updateById(alertData);
        alertData = alertDataMapper.selectById(alertData.getId());

        LambdaUpdateWrapper<AlertData> updateWrapper = Wrappers.<AlertData>lambdaUpdate().ne(AlertData::getId, alertData.getId())
                .eq(AlertData::getDataValue, alertData.getDataValue())
                .eq(AlertData::getCreateTime, alertData.getCreateTime())
                .set(AlertData::getStatus, 1)
                .set(AlertData::getProcessorId, userId)
                .set(AlertData::getProcessorName, name)
                .set(AlertData::getUpdateBy, alertData.getUpdateBy())
                .set(AlertData::getProcessingTime, alertData.getProcessingTime())
                .set(AlertData::getProcessingResult, alertData.getProcessingResult());

        int update = alertDataMapper.update(null, updateWrapper);
        if(update > 0 && result > 0){
            return 1;
        }else {
            return 0;
        }
    }

    private TableDataInfo<AlertData> creatTableData(Page<AlertData> page) {
        TableDataInfo<AlertData> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setMsg("请求成功");
        tableDataInfo.setCode(200);
        return tableDataInfo;
    }
}
